/*
 * @(#)DbConditionHolder.java $version 2010. 4. 8.
 *
 * Copyright 2007 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.mywep.myalimi.common;

import com.nhncorp.lucy.spring.core.util.DataMap;
import com.nhncorp.lucy.spring.core.util.ExtendedMap;
import com.nhncorp.lucy.spring.core.web.helper.PagerInfo;

/**
 * 1. 사용목적
 * -> ibator에 의해 생성된 코드에서 DataMap을 편리하게 생성하도록 도와주는 헬퍼 클래스
 * 2. 비고
 * ->
 *
 * @author 최창렬(iknowchang@nhncorp.com)
 * @author 이동국
 *
 */
public class DbConditionHolder {
	public static ExtendedMap getInstance() {
		return new DataMap();
	}

	public static ExtendedMap getRawPageInstance(PagerInfo pagerInfo, String orderByClause) {
		ExtendedMap instance = new DataMap();

		if (pagerInfo != null) {
			if (pagerInfo.getStartRownum() > 0) {
				instance.put("limitOffset", (pagerInfo.getRawPage() - 1) * pagerInfo.getPageSize());
			}
			if (pagerInfo.getPageSize() > 0) {
				instance.put("limitMaxRows", pagerInfo.getPageSize());
			}
		}
		if (orderByClause != null) {
			instance.put("orderByClause", orderByClause);
		}

		return instance;
	}

	/**
	 * 페이지 처리및 정렬조건을 가지는 DataMap 반환
	 * 
	 * @param limitOffset .
	 * @param limitMaxRows .
	 * @param orderByClause .
	 * @return .
	 */
	public static ExtendedMap getInstance(Integer limitOffset, Integer limitMaxRows, String orderByClause) {
		ExtendedMap instance = new DataMap();

		if (limitOffset != null) {
			instance.put("limitOffset", limitOffset);
		}
		if (limitMaxRows != null) {
			instance.put("limitMaxRows", limitMaxRows);
		}
		if (orderByClause != null) {
			instance.put("orderByClause", orderByClause);
		}

		return instance;
	}

	/**
	 * 정렬조건을 가지는 DataMap 반환
	 * 
	 * @param pagerInfo .
	 * @param orderByClause .
	 * @return .
	 */
	public static ExtendedMap getInstance(PagerInfo pagerInfo) {
		ExtendedMap instance = new DataMap();
		DbConditionHolder.addPagerInfo(instance, pagerInfo);

		return instance;
	}

	/**
	 * 정렬조건을 가지는 DataMap 반환
	 * 
	 * @param pagerInfo .
	 * @param orderByClause .
	 * @return .
	 */
	public static ExtendedMap getInstance(PagerInfo pagerInfo, String orderByClause) {
		ExtendedMap instance = new DataMap();
		DbConditionHolder.addPagerInfo(instance, pagerInfo);

		if (orderByClause != null) {
			instance.put("orderByClause", orderByClause);
		}

		return instance;
	}

	/**
	 * DataMap에 페이지  정보를 셋팅  
	 * 
	 * @param target .
	 * @param pagerInfo .
	 */
	public static void addPagerInfo(ExtendedMap target, PagerInfo pagerInfo) {
		if (pagerInfo != null) {
			if (pagerInfo.getStartRownum() > 0) {
				target.put("limitOffset", (pagerInfo.getPage() - 1) * pagerInfo.getPageSize());
			}
			if (pagerInfo.getPageSize() > 0) {
				target.put("limitMaxRows", pagerInfo.getPageSize());
			}
		}
	}
}
